package com.yunshan.mp.service.impl;

import com.yunshan.common.utils.DateUtils;
import com.yunshan.mp.domain.MpIndexActivityNotice;
import com.yunshan.mp.mapper.MpIndexActivityNoticeMapper;
import com.yunshan.mp.service.IMpIndexActivityNoticeService;
import com.yunshan.order.service.IOrderReserveNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 活动公告Service业务层处理
 * 
 * @author yunshan
 * @date 2025-01-20
 */
@Service
public class MpIndexActivityNoticeServiceImpl implements IMpIndexActivityNoticeService 
{
    @Autowired
    private MpIndexActivityNoticeMapper mpIndexActivityNoticeMapper;
    @Autowired
    private IOrderReserveNoticeService orderReserveNoticeService;

    /**
     * 查询活动公告
     * 
     * @param id 活动公告主键
     * @return 活动公告
     */
    @Override
    public MpIndexActivityNotice selectMpIndexActivityNoticeById(Long id)
    {
        MpIndexActivityNotice mpIndexActivityNotice = mpIndexActivityNoticeMapper.selectMpIndexActivityNoticeById(id);
        if (mpIndexActivityNotice != null) {
            mpIndexActivityNotice.setReserveCount(orderReserveNoticeService.selectReserveCount(mpIndexActivityNotice.getId()));
        }
        return mpIndexActivityNotice;
    }

    /**
     * 查询活动公告列表
     * 
     * @param mpIndexActivityNotice 活动公告
     * @return 活动公告
     */
    @Override
    public List<MpIndexActivityNotice> selectMpIndexActivityNoticeList(MpIndexActivityNotice mpIndexActivityNotice)
    {
        List<MpIndexActivityNotice> mpIndexActivityNotices = mpIndexActivityNoticeMapper.selectMpIndexActivityNoticeList(mpIndexActivityNotice);
        for (MpIndexActivityNotice indexActivityNotice : mpIndexActivityNotices) {
            indexActivityNotice.setReserveCount(orderReserveNoticeService.selectReserveCount(indexActivityNotice.getId()));
        }

        return mpIndexActivityNotices;
    }

    /**
     * 新增活动公告
     * 
     * @param mpIndexActivityNotice 活动公告
     * @return 结果
     */
    @Override
    public int insertMpIndexActivityNotice(MpIndexActivityNotice mpIndexActivityNotice)
    {
        //判断上下架时间
        if (mpIndexActivityNotice.getOnTime() != null && mpIndexActivityNotice.getOffTime() != null) {
            if (mpIndexActivityNotice.getOnTime().before(DateUtils.getNowDate()) && mpIndexActivityNotice.getOffTime().after(DateUtils.getNowDate())) {
                mpIndexActivityNotice.setStatus(0);
            } else if (mpIndexActivityNotice.getOffTime().before(DateUtils.getNowDate())) {
                mpIndexActivityNotice.setStatus(1);
            }
        }
        mpIndexActivityNotice.setCreateTime(DateUtils.getNowDate());
        return mpIndexActivityNoticeMapper.insertMpIndexActivityNotice(mpIndexActivityNotice);
    }

    /**
     * 修改活动公告
     * 
     * @param mpIndexActivityNotice 活动公告
     * @return 结果
     */
    @Override
    public int updateMpIndexActivityNotice(MpIndexActivityNotice mpIndexActivityNotice)
    {
        mpIndexActivityNotice.setUpdateTime(DateUtils.getNowDate());
        return mpIndexActivityNoticeMapper.updateMpIndexActivityNotice(mpIndexActivityNotice);
    }

    /**
     * 批量删除活动公告
     * 
     * @param ids 需要删除的活动公告主键
     * @return 结果
     */
    @Override
    public int deleteMpIndexActivityNoticeByIds(Long[] ids)
    {
        return mpIndexActivityNoticeMapper.deleteMpIndexActivityNoticeByIds(ids);
    }

    /**
     * 删除活动公告信息
     * 
     * @param id 活动公告主键
     * @return 结果
     */
    @Override
    public int deleteMpIndexActivityNoticeById(Long id)
    {
        return mpIndexActivityNoticeMapper.deleteMpIndexActivityNoticeById(id);
    }

    @Override
    public List<String> selectCoverUniList() {
        return mpIndexActivityNoticeMapper.selectCoverUniList();
    }

    @Override
    public List<MpIndexActivityNotice> selectNoticeList() {
        return mpIndexActivityNoticeMapper.selectNoticeList();
    }

    @Override
    public int updateMpIndexActivityStatus(Integer status) {
        return mpIndexActivityNoticeMapper.updateMpIndexActivityStatus(status);
    }
}
